
using ABI.Microsoft.UI.Xaml.Controls;
using LifeGame2.Models;
using Button = Microsoft.Maui.Controls.Button;
using ColumnDefinition = Microsoft.Maui.Controls.ColumnDefinition;
using Grid = Microsoft.Maui.Controls.Grid;
using Image = Microsoft.Maui.Controls.Image;
using RowDefinition = Microsoft.Maui.Controls.RowDefinition;

namespace LifeGame2.Pages;

public class GamePage : ContentPage {
    private Grid baseLayout;
    private Grid map;
    private Image[,][] cells;
    public static int height = 200;
    public static int width = 300;
    public static int liveRate = 50;

    public bool isAuto = false;
    public Thread gameThread;

    public Entry HeightEntry;
    public Entry WidthEntry;
    public Entry LiveRateEntry;
    private Button buttonInit;
    private Button buttonNextTurn;
    private Button buttonStart;
    private Button buttonStop;

    public LifeGameCore gameCore;
    //public 
    public GamePage() {
        // row 0 is  white space;
        // row 1 is  Content;
        // row 2 is  white space;

        // col 0 is  Map;
        // col 1 is  Controller
        baseLayout = new Grid {
            RowDefinitions = {
                new RowDefinition {
                    Height = new GridLength(5, GridUnitType.Absolute)
                },
                new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Auto)
                },
                new RowDefinition {
                    Height = new GridLength(5, GridUnitType.Absolute)
                }
            },
            ColumnDefinitions = {
                new ColumnDefinition {
                    Width = new GridLength(25, GridUnitType.Star)
                },
                new ColumnDefinition {
                    Width = new GridLength(7, GridUnitType.Star)
                }
            }
        };

        map = new Grid();
        for (int i = 0;i < width;++i) {
            map.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Auto)
            });
        }

        for (int i = 0;i < height;++i) {
            map.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(1, GridUnitType.Auto)
            });
        }
        cells = new Image[height, width][];
        for (int i = 0;i < height;++i) {
            for (int j = 0;j < width;++j) {
                cells[i, j] = new Image[2];
                cells[i, j][0] = new Image { Source = "dead.png" };
                cells[i, j][1] = new Image { Source = "live.png" };
                map.Add(cells[i, j][1], j, i);
                map.Add(cells[i, j][0], j, i);
            }
        }
        Content = baseLayout;
        baseLayout.Add(map, 0, 1);
        gameCore = new ClassicConway(width, height);
        gameCore.RandomSeeding(liveRate);
        //UpdateMap();
        //isAuto = true;
        //gameThread = new Thread(new GameModel(this).AutoPlay);
    }

    void UpdateMap() {
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                if (gameCore.Curr[i, j]) {
                    if (cells[i, j][0].ZIndex > cells[i, j][1].ZIndex) {
                        (cells[i, j][0].ZIndex, cells[i, j][1].ZIndex) = (cells[i, j][1].ZIndex, cells[i, j][0].ZIndex);
                    }
                }else if (cells[i, j][0].ZIndex < cells[i, j][1].ZIndex){
                    (cells[i, j][0].ZIndex, cells[i, j][1].ZIndex) = (cells[i, j][1].ZIndex, cells[i, j][0].ZIndex);
                }
            }
        }
    }

    class GameModel {
        private GamePage page;

        public GameModel(GamePage page) {
            this.page = page;
        }
        public void NextTurn() {

        }

        public void AutoPlay() {
            while (page.isAuto) {
                page.gameCore.NextTurn();
                page.UpdateMap();
                Thread.Sleep(100);
            }

        }
    }
}